import {Component, Input, OnInit} from '@angular/core';
import {Page} from '../../../public/util/page';
import {SettingUrl} from '../../../public/setting/setting_url';
import {Enums, States, UserTypes} from '../../../public/setting/enums';
import {FormBuilder, FormGroup, Validators} from '@angular/forms';
import {UserService} from '../user.service';

@Component({
  selector: 'app-user-sub-account',
  templateUrl: './user-sub-account.component.html',
  styleUrls: ['./user-sub-account.component.css']
})
export class UserSubAccountComponent implements OnInit {
  public userList: Page = new Page(); //供应商
  public _loading: boolean = false;
  public routerLinks = SettingUrl.ROUTERLINK;//路由
  public states = States;       // 状态
  public validateForm: FormGroup;         //表单
  public userType = Enums.userType;       // 是否是领导人
  public userTypes = UserTypes;       // 用户类型
  @Input() userCode: string;

  constructor(private fb: FormBuilder, private userService: UserService) {
    this.validateForm = this.fb.group({
      password: [null, Validators.compose([Validators.required])], // 密码
    });
  }

  ngOnInit() {
    this.queryUserList();
  }

  /**
   * 查询user列表
   * @param curPage 当前页
   */
  queryUserList(curPage?: number) {
    this._loading = true;
    if (curPage) this.userList.curPage = curPage;//当有页码时，查询该页数据
    this.userList.params = {
      userCode: this.userCode,
      curPage: this.userList.curPage, //目标页码
      pageSize: 10, //每页条数
    };
    this.userService.getUserSubAccountList(this.userList.params).then((res: Page) => {
      this._loading = false;
      this.userList = res;
    }).catch(err => {
      this._loading = false;
    });
  }
}

